package com.venus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.venus.entity.Consultant;
import com.venus.entity.Sign;
import com.venus.entity.User;
import com.venus.service.ConsultantService;
import com.venus.util.PageUtil;
import com.venus.util.Result;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * 咨询师表 consultant 前端控制器
 * </p>
 *
 * @author Venus
 * @since 2020-06-08
 */
@RestController
@RequestMapping("/consultant")
public class ConsultantController {
    @Resource
    private ConsultantService consultantService;


    @RequestMapping("/select")
    public Object selectCon(Consultant consultant, PageUtil pu) {

        System.out.println(pu.getPage());
        System.out.println(pu.getLimit());

        //获取分页信息
        //getPage()为当前页码  pu.getLimit()为每页显示条数
        Page<Consultant> page = new Page<Consultant>(pu.getPage(), pu.getLimit());
        //创建实体包装类   用于存放查询条件
        QueryWrapper<Consultant> select = new QueryWrapper<Consultant>();
        //根据名字模糊查询
        if (consultant.getUserName() != null && !consultant.getUserName().isEmpty()) {
            select.like("user_name", consultant.getUserName());
        }
        if (consultant.getIsOpen() != null && !consultant.getIsOpen().isEmpty()) {
            select.like("is_open", consultant.getIsOpen());
        }
        /*遗留签到时间的查询*/

        //分页查询方法
        page = consultantService.page(page, select);
        //getRecords()获取page中的集合数据
        //getTotal()获取总条数
        return new Result(0, "success", page.getRecords(), page.getTotal());
    }

    @RequestMapping("/update")
    public Object updateCon(Consultant consultant) {
        boolean update = consultantService.updateById(consultant);
        if (update == true) {
            return new Result(0, "修改陈功");
        } else {
            return new Result(1, "修改失败！");
        }
    }

    @RequestMapping("/isOpen")
    public Object updateOpen(Consultant consultant) {

        if (consultant.getIsOpen().equals("1")) {
            consultant.setIsOpen("0");
            boolean update = consultantService.updateById(consultant);
            if (update == true) {
                return new Result(0, "已关闭");
            } else {
                return new Result(2, "出问题了，刷新试试吧！");
            }
        } else {
            consultant.setIsOpen("1");
            boolean update = consultantService.updateById(consultant);
            if (update == true) {
                return new Result(1, "已开启");
            } else {
                return new Result(2, "出问题了，刷新试试吧！");
            }
        }
    }





}
